﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Csv2Json
{
    public class JsonConverter
    {
        private StreamReader _streader;
        private StreamWriter _strwriter;

        public JsonConverter(String path, String outpath)
        {
            _streader = new StreamReader(new FileStream(path, FileMode.Open));
            _strwriter = new StreamWriter(new FileStream(outpath, FileMode.Create));
        }

        public void Convert()
        {
            print("Starting to write ...");
            _strwriter.WriteLine("[");
            string[] headers = _streader.ReadLine().Split(',');
            
            while (!_streader.EndOfStream)
            {
                string[] line = _streader.ReadLine().Split(',');
                string temp = "";
                for (int i = 0; i < headers.Length; i++)
                {
                    temp+= "\"" + headers[i] + "\":\"" + line[i]+ "\",";
                }
                temp = temp.Substring(0, temp.Length - 1);
                _strwriter.WriteLine("{"+temp+"}");
            }
            _strwriter.WriteLine("]");
            print("Write complete. ");
        }

        public void print(String str)
        {
            Console.WriteLine(str);
        }
    }
}
